Sunburst plot visualizes stratified data gradually from roots to leaves.
The root starts from the center and squirt are added to the outer rings.
Each level of the hierarchy is represented by one ring or circle with the innermost circle, further rings are divided into slices that represent data points and the size of the slice represents data values.
library(sunburstR)
packageVersion("sunburstR")[1] '2.1.6'
#> [1] '2.1.6'
library(htmltools)
library(d3r)
#Simple data should suffice for these examples.dat <- data.frame(
level1 = rep(c("a", "b"), each=3),
level2 = paste0(rep(c("a", "b"), each=3), 1:3),
size = c(10,5,2,3,8,6),
stringsAsFactors = FALSE
)knitr::kable(dat)| level1 | level2 | size |
|---|---|---|
| a | a1 | 10 |
| a | a2 | 5 |
| a | a3 | 2 |
| b | b1 | 3 |
| b | b2 | 8 |
| b | b3 | 6 |
d3r will help us build our hierarchy.
library(d3r)
tree <- d3_nest(dat, value_cols = "size")
tree{"children":[{"name":"a","children":[{"name":"a1","size":10,"colname":"level2"},{"name":"a2","size":5,"colname":"level2"},{"name":"a3","size":2,"colname":"level2"}],"colname":"level1"},{"name":"b","children":[{"name":"b1","size":3,"colname":"level2"},{"name":"b2","size":8,"colname":"level2"},{"name":"b3","size":6,"colname":"level2"}],"colname":"level1"}],"name":"root"}
Often the legend in the sunburst becomes useless with lots of nodes and multiple levels. While a hack could turn it off, I don’t like having to ask users to resort to hacks, so now the argument legend = FALSE will turn it off.
legend = FALSEsb1 <- sunburst(
tree,
width="100%",
height=400
)
sb1sb2 <- sunburst(
tree,
legend = FALSE,
width = "100%",
height = 400
)
sb2div( style=“display: flex; align-items:center;”, div(style=“width:50%; border:1px solid #ccc;”, sb1), div(style=“width:50%; border:1px solid #ccc;”, sb2) )
How to make sunburst charts in R with Plotly.
library(plotly)fig <- plot_ly(
labels = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"),
parents = c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
values = c(10, 14, 12, 10, 2, 6, 6, 4, 4),
type = 'sunburst'
)figWith branchvalues “total”, the value of the parent represents the width of its wedge. In the example below, “Enoch” is 4 and “Awan” is 6 and so Enoch’s width is 4/6ths of Awans. With branchvalues “remainder”, the parent’s width is determined by its own value plus those of its children. So, Enoch’s width is 4/10ths of Awan’s (4 / (6 + 4)).
Note that this means that the sum of the values of the children cannot exceed the value of their parent when branchvalues “total”. When branchvalues “relative” (the default), children will not take up all of the space below their parent (unless the parent is the root and it has a value of 0).
library(plotly)
fig <- plot_ly(
labels = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"),
parents = c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
values = c(65, 14, 12, 10, 2, 6, 6, 4, 4),
type = 'sunburst',
branchvalues = 'total'
)figlibrary(plotly)
d <- data.frame(
ids = c(
"North America", "Europe", "Australia", "North America - Football", "Soccer",
"North America - Rugby", "Europe - Football", "Rugby",
"Europe - American Football","Australia - Football", "Association",
"Australian Rules", "Autstralia - American Football", "Australia - Rugby",
"Rugby League", "Rugby Union"
),
labels = c(
"North<br>America", "Europe", "Australia", "Football", "Soccer", "Rugby",
"Football", "Rugby", "American<br>Football", "Football", "Association",
"Australian<br>Rules", "American<br>Football", "Rugby", "Rugby<br>League",
"Rugby<br>Union"
),
parents = c(
"", "", "", "North America", "North America", "North America", "Europe",
"Europe", "Europe","Australia", "Australia - Football", "Australia - Football",
"Australia - Football", "Australia - Football", "Australia - Rugby",
"Australia - Rugby"
),
stringsAsFactors = FALSE
)
fig <- plot_ly(d, ids = ~ids, labels = ~labels, parents = ~parents, type = 'sunburst')figThe insidetextorientation attribute controls the orientation of text inside sectors.
With “auto” the texts may automatically be rotated to fit with the maximum size inside the slice. Using “horizontal” (resp. “radial”, “tangential”) forces text to be horizontal (resp. radial or tangential). Note that plotly may reduce the font size in order to fit the text with the requested orientation.
df = read.csv("https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/coffee-flavors.csv")
fig <- plot_ly()fig <- fig %>% add_trace(
type='sunburst',
ids=df$ids,
labels=df$labels,
parents=df$parents,
domain=list(column=1),
maxdepth=2,
insidetextorientation='radial'
)figIn order to create sunburst chart subplots, we use the domain attribute and the layout grid attribute.
library(plotly)
d1 <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/coffee-flavors.csv")
d2 <- read.csv("https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv")fig <- plot_ly()
fig <- fig %>%
add_trace(
ids = d1$ids,
labels = d1$labels,
parents = d1$parents,
type = 'sunburst',
maxdepth = 2,
domain = list(column = 0)
) fig <- fig %>%
add_trace(
ids = d2$ids,
labels = d2$labels,
parents = d2$parents,
type = 'sunburst',
maxdepth = 3,
domain = list(column = 1)
)
fig <- fig %>%
layout(
grid = list(columns =2, rows = 1),
margin = list(l = 0, r = 0, b = 0, t = 0),
sunburstcolorway = c(
"#636efa","#EF553B","#00cc96","#ab63fa","#19d3f3",
"#e763fa", "#FECB52","#FFA15A","#FF6692","#B6E880"
),
extendsunburstcolors = TRUE)figSee https://plotly.com/r/reference/#sunburst for more information and chart attribute options! What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)